<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<%@include file="/common/taglibs.jsp"%>
<%@include file="/common/javascript.jsp"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<title>获取坐标</title>
<style type="text/css">
html {
	height: 100%
}

body {
	height: 100%;
	margin: 0px;
	padding: 0px
}

#allmap {
	height: 100%
}
</style>
<script
	src="http://api.map.baidu.com/api?v=1.5&ak=FD619b3a912624d6d79233bbf0709528"
	type="text/javascript"></script>
<!--加载鼠标绘制工具-->
<script type="text/javascript"
	src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
<link rel="stylesheet"
	href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css" />

<script type="text/javascript">
    	var viewType='${viewType}';
    	var operateType='${operateType}';
    	var type='${type}';
    	var markname='${markerName}';
    	var points = [];
    	var strlng;
		var strlat;
		var strPoints;
		
		var center;
		var str;
		var d;
		var map;
		var icon=new BMap.Icon('images/marker/'+markname,new BMap.Size(36, 36),
    		 { anchor: new BMap.Size(18, 18)});
		
		$(document).ready(function(e) {
            map = new BMap.Map("allmap");            // 创建Map实例
            
			map.enableScrollWheelZoom();  //启用滚轮放大缩小
			map.enableContinuousZoom();    // 开启连续缩放效果
			map.enableInertialDragging();// 开启惯性拖拽效果
			map.addControl(new BMap.NavigationControl());     
            map.addControl(new BMap.ScaleControl());
            
            if(type=='1'){
            	strlng='${cityStatistics.lon}';
            	strlat='${cityStatistics.lat}';
            	strPoints='${cityStatistics.points}';
            }else if(type=='2'){
            	strlng='${regionStatistics.lon}';
            	strlat='${regionStatistics.lat}';
            	strPoints='${regionStatistics.points}';
            }else{
            	strlng='${landEntity.lng}';
            	strlat='${landEntity.lat}';
            	strPoints='${landEntity.points}';
            }
            
            var point=new BMap.Point(parseFloat(strlng),parseFloat(strlat));
            
            if(operateType=='getloc'){  //获取坐标 
            	//初始化位置坐标
            	if(viewType!='add'){
            		var marker1 = new BMap.Marker(point,{icon:icon});
					marker1.enableDragging(); //marker可拖拽
					map.addOverlay(marker1);
					marker1.addEventListener("dragend", function(e){
						d=e;   
					});
					map.centerAndZoom(point,16);
            	}else{
            		map.clearOverlays();
            		map.centerAndZoom('${ecity.name}');
            		
            		map.addEventListener("click", mapAdd);
            	}
            }else if(operateType=='getPolygon'){
            	if(viewType!='add'){
            		points=strPoints.split("|");
            		var pts=[];
					for(var j=0;j<points.length;j++){
						var pstr=points[j].split(",");
						var pt=new BMap.Point(pstr[0],pstr[1]);
						pts.push(pt);
					}
					var marker1=new BMap.Polygon(pts,{strokeColor:"blue",fillColor:"red", strokeWeight:2, strokeOpacity:0.2});
					map.addOverlay(marker1);
					
					marker1.enableEditing(); //图形可编辑
					marker1.addEventListener("lineupdate", function(e){
						points=e.target.getPath();
						str="";
            			for(var j=0;j<points.length;j++){
            				var p=points[j];
            				str=str+p.lng+","+p.lat+"|";
            			}
            			str=str.substring(0, str.length-1);
            			center=e.target.getBounds().getCenter();
					});
					
					map.centerAndZoom(point,16);
					
            	}else{
            		map.clearOverlays();
            		map.centerAndZoom('${ecity.name}');
            		
            		var styleOptions = {
        				strokeColor:"red",    //边线颜色。
        				fillColor:"red",      //填充颜色。当参数为空时，圆形将没有填充效果。
        				strokeWeight: 3,       //边线的宽度，以像素为单位。
        				strokeOpacity: 0.8,	   //边线透明度，取值范围0 - 1。
        				fillOpacity: 0.6,      //填充的透明度，取值范围0 - 1。
        				strokeStyle: 'solid' //边线的样式，solid或dashed。
    				};
            		
            		var polygon;
            		var overlaycomplete = function(e){
            			points=e.overlay.getPath();
            			str="";
            			for(var j=0;j<points.length;j++){
            				var p=points[j];
            				str=str+p.lng+","+p.lat+"|";
            			}
            			str=str.substring(0, str.length-1);
            			polygon=e.overlay;
            			center=polygon.getBounds().getCenter();
            			myDrawingManagerObject.disableCalculate();
            			myDrawingManagerObject.close();
            			points=[];
            			area=0;
            			
            			polygon.enableEditing(); //图形可编辑
            			polygon.addEventListener("lineupdate", function(e){
							points=e.target.getPath();
							str="";
            				for(var j=0;j<points.length;j++){
            					var p=points[j];
            					str=str+p.lng+","+p.lat+"|";
            				}
            				str=str.substring(0, str.length-1);
            				center=e.target.getBounds().getCenter();
						});
            			
            		};
            	
            		//实例化鼠标绘制工具
    				var myDrawingManagerObject  = new BMapLib.DrawingManager(map, {
        				isOpen: true, //是否开启绘制模式
        				enableDrawingTool: false, //是否显示工具栏
        				polygonOptions: styleOptions //多边形的样式
    				});
    				myDrawingManagerObject.setDrawingMode(BMAP_DRAWING_POLYGON);
    				myDrawingManagerObject.open();
            		//添加鼠标绘制工具监听事件，用于获取绘制结果
    				myDrawingManagerObject .addEventListener('overlaycomplete', overlaycomplete);
            	}
            }
        });
        
        function submit(){
        	var win=window.parent.window;
        	if(operateType=='getloc'){
        		win.setLonlat(d);
        	}else{
        		win.setArea(str,center);
        	}
        }
        
       
		function mapAdd(e) {
			map.clearOverlays();
			d = e;
			var marker1 = new BMap.Marker(e.point,{icon:icon});
			marker1.enableDragging(); //marker可拖拽
			marker1.addEventListener("dragend", function(e) {
				d = e;
			});
			map.addOverlay(marker1);
		}

	//重绘点或者多边形
	function clear() {
		map.clearOverlays();
		if (operateType == 'getloc') {
			map.removeEventListener("click", mapAdd);
			map.addEventListener("click", mapAdd);
		} else {
			var styleOptions = {
				strokeColor : "red", //边线颜色。
				fillColor : "red", //填充颜色。当参数为空时，圆形将没有填充效果。
				strokeWeight : 3, //边线的宽度，以像素为单位。
				strokeOpacity : 0.8, //边线透明度，取值范围0 - 1。
				fillOpacity : 0.6, //填充的透明度，取值范围0 - 1。
				strokeStyle : 'solid' //边线的样式，solid或dashed。
			};

			var polygon;
			var overlaycomplete = function(e) {
				points = e.overlay.getPath();
				str = "";
				for ( var j = 0; j < points.length; j++) {
					var p = points[j];
					str = str + p.lng + "," + p.lat + "|";
				}
				str = str.substring(0, str.length - 1);
				polygon = e.overlay;
				center = polygon.getBounds().getCenter();
				myDrawingManagerObject.disableCalculate();
				myDrawingManagerObject.close();
				points = [];
				area = 0;

				polygon.enableEditing(); //图形可编辑
				polygon.addEventListener("lineupdate", function(e) {
					points = e.target.getPath();
					str = "";
					for ( var j = 0; j < points.length; j++) {
						var p = points[j];
						str = str + p.lng + "," + p.lat + "|";
					}
					str = str.substring(0, str.length - 1);
					center = e.target.getBounds().getCenter();
				});

			};

			//实例化鼠标绘制工具
			var myDrawingManagerObject = new BMapLib.DrawingManager(map, {
				isOpen : true, //是否开启绘制模式
				enableDrawingTool : false, //是否显示工具栏
				polygonOptions : styleOptions
			//多边形的样式
			});
			myDrawingManagerObject.setDrawingMode(BMAP_DRAWING_POLYGON);
			myDrawingManagerObject.open();
			//添加鼠标绘制工具监听事件，用于获取绘制结果
			myDrawingManagerObject.addEventListener('overlaycomplete',
					overlaycomplete);
		}
	}
</script>
</head>

<body style="height: 100%;width: 100%;">
	<div id="allmap" style="height: 100%;width: 100%;"></div>
</body>
</html>
